//
//  DialogViewController.swift
//  UserApp_iOS
//
//  Created by linlin on 2019/9/18.
//  Copyright © 2019 linlin. All rights reserved.
//

import UIKit

enum DialogStyle:Int {
    case actionsheet
    case alert
}

protocol DialogDataSource: AnyObject {
    func dialogContentView() -> UIView
}

class BaseDialogView: UIView {
    
    init(style: DialogStyle = .alert) {
        super.init(frame: CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight))

        dialogStyle = style
        self.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.1)
        self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(backgroundClickAction)))
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    @objc func backgroundClickAction() -> Void {
        if canceledOnClickOutside {
            dismiss()
        }
    }
    
    weak var dataSource: DialogDataSource?
    var canceledOnClickOutside: Bool = false
    private var dialogStyle: DialogStyle = .alert
    private var isShowing: Bool = false
    private var duration: TimeInterval = 0.3
    private var contentView: UIView?

    func show() -> Void {
        if isShowing {
            return
        }
        
        if let _ = contentView {
        } else {
            if let dataS = dataSource {
                contentView = dataS.dialogContentView()
                self.addSubview(contentView!)
                if dialogStyle == .alert {
                    contentView?.top = (self.height - contentView!.height)/2
                } else {
                    contentView?.bottom = self.height
                }
                contentView?.left = (self.width - contentView!.width)/2
            }
        }
        
        appDelegate.window?.addSubview(self)
        switch dialogStyle {
        case .alert:
            self.alpha = 0
            UIView.animate(withDuration: duration
                , animations: { [unowned self] in
                    self.alpha = 1
            }) {[unowned self] (complete) in
                self.isShowing = true
            }
            
        default:
            self.contentView?.top = self.height
            self.alpha = 0
            UIView.animate(withDuration: duration
                , animations: { [unowned self] in
                    self.alpha = 1
                    self.contentView?.bottom = self.height
            }) {[unowned self] (complete) in
                self.isShowing = true
            }
        }
    }
    
    func dismiss() -> Void {
        if !isShowing {
            return
        }
        
        switch dialogStyle {
        case .alert:
            UIView.animate(withDuration: duration
                , animations: { [unowned self] in
                    self.alpha = 0
            }) {[unowned self] (complete) in
                self.removeFromSuperview()
                self.isShowing = false
            }
        default:
            UIView.animate(withDuration: duration
                , animations: { [unowned self] in
                    self.alpha = 0
                    self.contentView?.top = self.height
            }) {[unowned self] (complete) in
                self.removeFromSuperview()
                self.isShowing = false
            }
        }
    }
}

